#!/usr/bin/python
# plot_LumFunc.py

import numpy
from matplotlib import use as useBackend
useBackend('Agg')
import matplotlib.pyplot as ppt


def cheat111():
    '''Cheat to clear plot.'''
    ppt.subplot(131)
    ppt.plot([0,1],[1,2])
    ppt.subplot(311)
    ppt.plot([0,1],[1,2])
    ppt.subplot(111)
    ppt.hold(True)


def plot(simpars, phypars, distriblist):
    '''Shows a slice of GM_LumFunc's results.'''
    for i in simpars:
        cmd = "%s = simpars['%s']" % (i,i)
        exec cmd
    for i in phypars:
        cmd = "%s = phypars['%s']" % (i,i)
        exec cmd
    dmax = int((scale + R0)/1000. + 0.001)
    bstring = 'Galactic latitude'
    dstring = 'Distance from sun, kpc'
    lstring = 'Galactic longitude'
    lims_ld = [0, dmax, 0, 180]
    lims_bd1 = [0, dmax, 0, 90]
    lims_bd2 = [0, dmax, 0, 90]
    asp_ld = 0.6*dmax/180.
    asp_bd = 0.6*dmax/90.
    maxchars = 0
    for i in range(0, len(distriblist)):
        nchars = len(str(MvList[i]))
        if nchars > maxchars:
            maxchars = nchars
#            maxstring = str(MvList[i])
    # Plot slices for each population
    for i in range(0, len(distriblist)):
        stars = distriblist[i]/delMvList[i] # Normalization
        magstring = str(MvList[i])
        nchars = len(magstring)
        l = maxchars - nchars
        if magstring.count('.') != 0:
            magstring = magstring + "0"*l
        else:
            magstring = "0"*l + magstring
        plotnameld = outdir+"Stars"+magstring+"ldPlane.pdf"
        plotnamebd1 = outdir+"Stars"+magstring+"bdPlaneLeeward.pdf"
        plotnamebd2 = outdir+"Stars"+magstring+"bdPlaneWindward.pdf"
        plottitleld = 'Density of stars with magnitude '+magstring+'''
in the galactic plane'''
        plottitlebd1 = 'Density of stars with magnitude '+magstring+'''
away from the galactic center'''
        plottitlebd2 = 'Density of stars with magnitude '+magstring+'''
towards the galactic center'''

        cheat111()
        ppt.imshow(stars[:,:,0].T, origin='lower', extent=lims_ld, aspect=asp_ld)
        ppt.title(plottitleld)
        ppt.colorbar(shrink=0.8)
        ppt.xlabel(dstring)
        ppt.ylabel(bstring)
        ppt.savefig(plotnameld)

        cheat111()
        ppt.imshow(stars[:,-1,:].T, origin='lower', extent=lims_bd1, aspect=asp_bd)
        ppt.title(plottitlebd1)
        ppt.colorbar(shrink=0.8)
        ppt.xlabel(dstring)
        ppt.ylabel(bstring)
        ppt.savefig(plotnamebd1)

        cheat111()
        ppt.imshow(stars[:,0,:].T, origin='lower', extent=lims_bd2, aspect=asp_bd)
        ppt.title(plottitlebd2)
        ppt.colorbar(shrink=0.8)
        ppt.xlabel(dstring)
        ppt.ylabel(bstring)
        ppt.savefig(plotnamebd2)
